package org.springblade.modules.restaurant.job;

import cn.hutool.core.date.DateUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.aizuda.snailjob.common.log.SnailJobLog;
import lombok.AllArgsConstructor;
import org.springblade.modules.restaurant.entity.TaskRecord;
import org.springblade.modules.restaurant.enums.TaskRecordStatusEnums;
import org.springblade.modules.restaurant.service.ITaskRecordService;
import org.springframework.stereotype.Component;

/**
 * 将未填写的任务状态设置为已过期
 *
 * @author harry
 * @公众号 Harry技术
 */
@Component
@AllArgsConstructor
@JobExecutor(name = "taskRecordSetExpireJobExecutor")
public class TaskRecordSetExpireJobExecutor {
	private final ITaskRecordService taskRecordService;

	public ExecuteResult jobExecute(JobArgs jobArgs) {
		SnailJobLog.REMOTE.info("将未填写的任务状态设置为已过期,jobArgs:{}", jobArgs);

		// 获取截止到当前 deadline 小于昨天时间的 数据
		taskRecordService.lambdaUpdate()
			.le(TaskRecord::getDeadline, DateUtil.yesterday())
			.eq(TaskRecord::getStatus, TaskRecordStatusEnums.WAIT_SUBMIT.getCode())
			.set(TaskRecord::getStatus, TaskRecordStatusEnums.EXPIRE.getCode()).update();

		// 将提交未评分的任务设置为未检查 deadline 小于昨天时间的 数据
		taskRecordService.lambdaUpdate()
			.le(TaskRecord::getDeadline, DateUtil.yesterday())
			.eq(TaskRecord::getStatus, TaskRecordStatusEnums.SUBMITTED.getCode())
			.set(TaskRecord::getStatus, TaskRecordStatusEnums.NO_CHECK.getCode()).update();
		return ExecuteResult.success();
	}


//	@PostConstruct
	public void init() {

		// 获取截止到当前 deadline 小于当前时间的 数据
		taskRecordService.lambdaUpdate()
			.le(TaskRecord::getDeadline, DateUtil.now())
			.eq(TaskRecord::getStatus, TaskRecordStatusEnums.WAIT_SUBMIT.getCode())
			.set(TaskRecord::getStatus, TaskRecordStatusEnums.EXPIRE.getCode()).update();

		// 将待提交的任务设置为未检查
		taskRecordService.lambdaUpdate()
			.le(TaskRecord::getDeadline, DateUtil.now())
			.eq(TaskRecord::getStatus, TaskRecordStatusEnums.SUBMITTED.getCode())
			.set(TaskRecord::getStatus, TaskRecordStatusEnums.NO_CHECK.getCode()).update();
	}
}
